package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.annotation.Login;
import com.atguigu.tingshu.common.annotation.TingShuCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.atguigu.tingshu.vo.order.TrackOrderVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {
    @Resource
    private TrackInfoService trackInfoService;
    @Resource
    private VodService vodService;

    @PostMapping("uploadTrack")
    @Operation(summary = "上传音频至vod")
    public Result<Map<String, Object>> uploadTrack(MultipartFile file) {
        Map<String, Object> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    @Login
    @PostMapping("saveTrackInfo")
    @Operation(summary = "新增音频")
    public Result saveTrackInfo(@Validated @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    @Login
    @PostMapping("findUserTrackPage/{page}/{limit}")
    @Operation(summary = "分页查询音频信息")
    public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody TrackInfoQuery query) {
        Page<TrackListVo> iPage = new Page<>(page, limit);
        Page<TrackListVo> trackListVoPage = trackInfoService.findUserTrackPage(iPage, query);
        return Result.ok(trackListVoPage);
    }

    @Login
    @DeleteMapping("removeTrackInfo/{id}")
    @Operation(summary = "根据id删除音频")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }

    @GetMapping("getTrackInfo/{id}")
    @Operation(summary = "根据id查询音频")
    @TingShuCache(cachePrefix = RedisConstant.TRACK_INFO_PREFIX,
            bloomFilter = RedisConstant.ALBUM_BLOOM_FILTER,
            lockPrefix = RedisConstant.TRACK_LOCK_PREFIX)
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    @Login
    @PutMapping("updateTrackInfo/{id}")
    @Operation(summary = "根据id修改音频")
    public Result<TrackInfo> updateTrackInfo(@PathVariable Long id, @Validated @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    @GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
    @Operation(summary = "根据专辑id分页查询声音列表")
    @Login(required = false)
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId, @PathVariable Integer page, @PathVariable Integer limit) {
        Page<AlbumTrackListVo> iPage = new Page<>(page, limit);
        Page<AlbumTrackListVo> trackInfoPage = trackInfoService.findAlbumTrackPage(iPage, albumId);
        return Result.ok(trackInfoPage);
    }

    @Operation(summary = "根据id查询声音详情")
    @GetMapping("/getTrackStatVo/{id}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long id) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(id);
        return Result.ok(trackStatVo);
    }

    @GetMapping("getPlayToken/{trackId}")
    @Operation(summary = "获取声音的播放凭证")
    @Login(required = false)
    public Result<Map<String, Object>> getPlayToken(@PathVariable Long trackId) {
        Map<String, Object> map = trackInfoService.getPlayToken(trackId);
        return Result.ok(map);
    }

    @GetMapping("findUserTrackPaidList/{trackId}")
    public Result<List<TrackOrderVo>> findUserTrackPaidList(@PathVariable Long trackId) {
        List<TrackOrderVo> trackOrderVos = this.trackInfoService.findUserTrackPaidList(trackId);
        return Result.ok(trackOrderVos);
    }

    @GetMapping("findPaidTrack/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrack(@PathVariable Long trackId, @PathVariable Integer trackCount) {
        List<TrackInfo> trackInfos = this.trackInfoService.findPaidTrack(trackId, trackCount);
        return Result.ok(trackInfos);
    }
}

